package com.architecture.core.service.associate;

import com.architecture.core.entity.Permission;
import com.architecture.core.entity.PermissionRole;
import com.architecture.core.except.ErrException;
import com.architecture.core.service.AbstractService;
import com.architecture.core.utils.ErrCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: aguang
 * Date: 13-1-21
 * Time: 下午6:13
 * To change this template use File | Settings | File Templates.
 */
@Service("managePermissionRoleService")
public class PermissionRoleAssociateService extends AbstractService {

    public void associatePermission(String permissionRoleId, String permissionId) throws ErrException {
        if (StringUtils.isBlank(permissionRoleId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        if (StringUtils.isBlank(permissionId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        PermissionRole permissionRole = (PermissionRole) super.baseDAO.findUnique(PermissionRole.class, permissionRoleId);
        Permission permission = (Permission) super.baseDAO.findUnique(Permission.class, permissionId);

        permissionRole.getPermissionList().add(permission);
        permission.getPermissionRoleList().add(permissionRole);

        super.baseDAO.update(permissionRole);
        super.baseDAO.update(permission);


    }

    public void associatePermission(String permissionRoleId, List<String> permissionIdList) throws ErrException {

        if (CollectionUtils.isEmpty(permissionIdList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        for (String permissionId : permissionIdList) {
            this.associatePermission(permissionRoleId,permissionId);
        }
    }

    public void deassociatePermission(String permissionRoleId, String permissionId) throws ErrException {
        if (StringUtils.isBlank(permissionRoleId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        if (StringUtils.isBlank(permissionId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        PermissionRole permissionRole = (PermissionRole) super.baseDAO.findUnique(PermissionRole.class, permissionRoleId);
        Permission permission = (Permission) super.baseDAO.findUnique(Permission.class, permissionId);

        permissionRole.getPermissionList().remove(permission);
        permission.getPermissionRoleList().remove(permissionRole);

        super.baseDAO.update(permissionRole);
        super.baseDAO.update(permission);
    }

    public void deassociatePermission(String permissionRoleId, List<String> permissionIdList) throws ErrException {
        if (CollectionUtils.isEmpty(permissionIdList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        for (String permissionId : permissionIdList) {
            this.deassociatePermission(permissionRoleId, permissionId);
        }
    }

    public void associatePermissionRole(String permissionRoleId, String subPermissionRoleId) throws ErrException {
        if (StringUtils.isBlank(permissionRoleId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        if (StringUtils.isBlank(subPermissionRoleId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        PermissionRole permissionRole = (PermissionRole) super.baseDAO.findUnique(PermissionRole.class, permissionRoleId);
        PermissionRole subPermissionRole = (PermissionRole) super.baseDAO.findUnique(PermissionRole.class, subPermissionRoleId);

        permissionRole.getPermissionRoleList().add(subPermissionRole);
        subPermissionRole.setPermissionRole(permissionRole);

        super.baseDAO.update(permissionRole);
        super.baseDAO.update(subPermissionRole);


    }

    public void associatePermissionRole(String permissionRoleId, List<String> subPermissionRoleIdList) throws ErrException {
        if (CollectionUtils.isEmpty(subPermissionRoleIdList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        for (String subPermissionRoleId : subPermissionRoleIdList) {
            this.associatePermissionRole(permissionRoleId,subPermissionRoleId);
        }
    }

    public void deassociateSubPermissionRole(String permissionRoleId,String subPermissionRoleId) throws ErrException {
        if (StringUtils.isBlank(subPermissionRoleId)) {
            throw new ErrException(ErrCode.BLANK_ID);
        }

        PermissionRole permissionRole = (PermissionRole) super.baseDAO.findUnique(PermissionRole.class, permissionRoleId);
        PermissionRole subPermissionRole = (PermissionRole) super.baseDAO.findUnique(PermissionRole.class, subPermissionRoleId);

        permissionRole.getPermissionRoleList().remove(subPermissionRole);
        subPermissionRole.setPermissionRole(null);

        super.baseDAO.update(permissionRole);
        super.baseDAO.update(subPermissionRole);

    }

    public void deassociateSubPermissionRole(String permissionRoleId,List<String> subPermissionRoleIdList) throws ErrException {
        if (CollectionUtils.isEmpty(subPermissionRoleIdList)) {
            throw new ErrException(ErrCode.EMPTY_COLLECTION);
        }

        for (String subPermissionRoleId : subPermissionRoleIdList) {
            this.deassociateSubPermissionRole(permissionRoleId,subPermissionRoleId);
        }
    }
}
